from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.routers import pdf
from app.config import settings
import logging
from pathlib import Path

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler(Path("logs/app.log")),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)

app = FastAPI(
    title=settings.PROJECT_NAME,
    description="提供PDF处理相关的API接口",
    version="1.0.0",
    docs_url="/api/docs",
    redoc_url="/api/redoc"
)

# 配置CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(pdf.router, prefix="/api/pdf", tags=["PDF处理"])

@app.get("/health")
async def health_check():
    """健康检查接口"""
    return {"status": "healthy"}

@app.get("/version")
async def version():
    """获取API版本信息"""
    return {
        "version": "1.0.0",
        "name": settings.PROJECT_NAME
    }

@app.on_event("startup")
async def startup_event():
    logger.info("应用启动")

@app.on_event("shutdown")
async def shutdown_event():
    logger.info("应用关闭") 